package com.uam.utils.geo.buffer;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.LineString;
import org.locationtech.jts.operation.buffer.BufferOp;

/**
 * 线型缓冲区域实现类
 * @author: heguanglong
 * @date: 2020-12-26
 **/
public class LineBufferImpl extends BufferBase implements BufferStrategy {
    @Override
    public String addBuffer(String gps, double rd) {
        Geometry gfLineString = geometryFactory.createLineString(getCoordinate(gps));
        Geometry buffer = getBuff(gfLineString, BufferOp.CAP_ROUND, rd);
        return gpsToString(buffer.getCoordinates());
    }

    @Override
    public String geometryInfo(String gps, double rd) {
        return  geometryFactory.createLineString(getCoordinate(gps)).toString();
    }

    @Override
    public Geometry geometry(String gps) {
        return geometryFactory.createLineString(getCoordinate(gps,""," "));
    }

    public Geometry geometry(Coordinate[] coordinates) {
        return geometryFactory.createLineString(coordinates);
    }

    public Geometry geometrySplit(String gps) {
        Coordinate[] coordinates=  getCoordinate(gps,";","\\,");
        if(coordinates.length<2){
            return null;
        }
        return geometryFactory.createLineString(coordinates);
    }
    public LineString geometryCoordinate(Coordinate[] gps) {
        return geometryFactory.createLineString(gps);
    }

    public LineString geometryCoordinate(JSONArray jsonArray) {
        int row=jsonArray.size();
        Coordinate[] gps=new Coordinate[row];
        for(int i=0;i<row;i++){
            JSONArray jsonArray1=jsonArray.getJSONArray(i);
            Coordinate coordinate=new Coordinate(jsonArray1.getDouble(0),jsonArray1.getDouble(1));
            gps[i]=coordinate;
        }
        return geometryCoordinate(gps);
    }

    public LineString getLineString(JSONObject root) {
        JSONObject t= root.getJSONObject("geometry");
        JSONArray jsonObject = t.getJSONArray("coordinates");
        return geometryCoordinate(jsonObject);
    }

    public LineString getLineStringGeometry(JSONObject root) {
        JSONArray jsonObject = root.getJSONArray("coordinates");
        return geometryCoordinate(jsonObject.getJSONArray(0));
    }
}
